package com.zstu.mall.web.admin;

import com.zstu.mall.entity.Product;
import com.zstu.mall.entity.ProductActivity;
import com.zstu.mall.entity.pojo.ResultBean;
import com.zstu.mall.service.ProductActivityService;
import com.zstu.mall.service.ProductService;
import com.zstu.mall.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author ：wuyc
 * @description：activityController
 * @date ：Created in 2020/1/3 0:06
 */
@Controller
@RequestMapping("/admin/activity")
public class AdminActivityController {

    @Autowired
    private ProductActivityService productActivityService;

    @Autowired
    private ProductService productService;

    /**
     * 返回列表页面
     *
     * @param
     * @return
     */
    @RequestMapping("/toList.html")
    public String toList() {
        return "mall/admin/activity/activityManage";
    }

    /**
     * 打开添加页面
     *
     * @return
     */
    @RequestMapping("/toAdd.html")
    public String toAdd() {
        return "admin/activity/add";
    }

    /**
     * 打开编辑页面
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/toEdit.html")
    public String toEdit(String id, Map<String, Object> map) {
        ProductActivity productActivity = productActivityService.get(id);
        List<Product> list = productService.findByActivityId(id, null);
        productActivity.setProducts(list);
        map.put("activity", productActivity);
        return "mall/admin/activity/activityUpdata";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/add.do")
    public String add(String activity, Double discount, String remark) throws Exception {
        ProductActivity productActivity = new ProductActivity();
        productActivity.setId(productActivityService.createId());
        productActivity.setActivity(activity);
        productActivity.setDiscount(discount);
        productActivity.setRemark(remark);
        productActivityService.create(productActivity);
        return "mall/admin/activity/activityManage";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/update.do")
    public String update(String id, String activity, Double discount, String remark) throws Exception {
        ProductActivity productActivity = productActivityService.get(id);
        productActivity.setActivity(activity);
        productActivity.setRemark(remark);
        productActivity.setDiscount(discount);
        productActivityService.update(productActivity);
        return "mall/admin/activity/activityManage";
    }

    @ResponseBody
    @RequestMapping("/del.do")
    public ResultBean<Boolean> del(String id) {
        productActivityService.delete(id);
        return new ResultBean<>(true);
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public ResultBean<List<ProductActivity>> findAll(int pageindex, @RequestParam(value = "pageSize", defaultValue = "15") int pageSize) {
        List<ProductActivity> productActivityList = productActivityService.findAll();
        List<Product> list = new ArrayList<>();
        for(ProductActivity productActivity : productActivityList){
            if (pageindex == -1)
                list = productService.findByActivityId(productActivity.getId(), null);
            else {
                Pageable pageable = PageRequest.of(pageindex, pageSize);
                list = productService.findByActivityId(productActivity.getId(), pageable);
            }
            productActivity.setProducts(list);
        }
        return new ResultBean<>(productActivityList);
    }
    
}
